// src/api/user.js
import axios from 'axios'
import { getAuthToken } from '@/router'

const instance = axios.create({
  baseURL: 'http://192.168.55.192:8080/api/user'
})

instance.interceptors.request.use(config => {
  config.headers.Authorization = `${getAuthToken()}`
  return config
})

export const userAPI = {
  // 获取用户信息
  getInfo: () => instance.get('/info'),
// 获取魔力值日志
  getMagicLogs: () => instance.get('/magic-log', {
  }),
  // 生成邀请码（免费）
  createInviteCode: () => instance.post('/invitation-code'),
  // 获取邀请码列表
  getInviteCodes: params => instance.get('/invitation-codes', { params }),
  getTorrentHistory: params => instance.get('/torrent-history', { params }),

  getMyMarathons: () => axios.get( 'http://192.168.55.192:8080/api/seeding-marathon/my-marathons',{
    headers: {
      Authorization: `${getAuthToken()}`
    }
  }),
  getMyBadges: () => axios.get('http://192.168.55.192:8080/api/seeding-marathon/my-badges', {
    headers: {
      Authorization: `${getAuthToken()}`
    }
  }),
  getMarathonStats: () => axios.get('http://192.168.55.192:8080/api/seeding-marathon/statistics', {
    headers: {
      Authorization: `${getAuthToken()}`
    }
  }),

  // 获取分享率状态
  getShareRatioStatus: () => instance.get('/share-ratio-status'),

  // 更新做种状态
  updateSeedingStatus: (torrentId, data) => instance.post(`/seeding/${torrentId}`, data),

  // 获取做种状态
  getSeedingStatus: () => instance.get('/seeding-status'),

  // 新增：使用魔力值生成邀请码
  createInviteCodeByMagic: () => axios.post('http://192.168.55.192:8080/api/activity/generate-invitation-code', {}, {
    headers: {
      Authorization: `${getAuthToken()}`
    }
  })
}